package com.gy.api.pc.filings.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.gy.api.base.Constants;
import com.gy.api.base.IDEntity;
import com.gy.api.base.Type;
import com.gy.api.mobile.company.dao.CompanyInfoDao;
import com.gy.api.mobile.company.domain.po.CompanyInfoPO;
import com.gy.api.mobile.msg.dao.UserMessagesDao;
import com.gy.api.pc.examine.dao.ExamineDao;
import com.gy.api.pc.examine.dao.ExamineLogDao;
import com.gy.api.pc.examine.domain.po.ExamineLogPO;
import com.gy.api.pc.examine.domain.po.ExaminePO;
import com.gy.api.pc.examine.domain.vo.*;
import com.gy.api.pc.examine.service.IExamineService;
import com.gy.api.pc.filings.dao.*;
import com.gy.api.pc.filings.domain.po.PatentApplicationAncillaryPO;
import com.gy.api.pc.filings.domain.po.PatentApplicationCompanyPO;
import com.gy.api.pc.filings.domain.po.PatentApplicationPO;
import com.gy.api.pc.filings.domain.po.ScriptRecordInfoPO;
import com.gy.api.pc.filings.domain.vo.*;
import com.gy.api.pc.filings.service.IPatentApplicationService;
import com.gy.api.pc.movie.dao.WcContentExamineDao;
import com.gy.api.pc.movie.domain.po.WcContentExaminePO;
import com.gy.api.pc.sys.user.dao.SysUserDao;
import com.gy.api.pc.sys.user.domain.po.SysUserPO;
import com.gy.exception.BusinessException;
import com.gy.task.UserMessageThread;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class PatentApplicationServiceImpl implements IPatentApplicationService {
    @Autowired
    ExamineDao examineDao;
    @Autowired
    ExamineLogDao examineLogDao;
    @Autowired
    private IExamineService examineService;
    @Autowired
    private CompanyInfoDao companyInfoDao;
    @Autowired
    private PatentApplicationDao applicationDao;
    @Autowired
    private PatentApplicationCompanyDao applicationCompanyDao;
    @Autowired
    private PatentApplicationAncillaryDao applicationAncillaryDao;
    @Autowired
    private PatentApplicationRecommendationDao applicationRecommendationDao;

    @Override
    public void addSave(WcPatentApplicationSaveVO saveVO) {
        //添加
        PatentApplicationPO patentApplicationPO=new PatentApplicationPO();
        BeanUtils.copyProperties(saveVO, patentApplicationPO);
        patentApplicationPO.setStatus(saveVO.getIsOutbox().equals("0")?1:0);
        patentApplicationPO.preInsert();
        applicationDao.insertSelective(patentApplicationPO);
        //添加附件
        PatentApplicationAncillaryPO patentApplicationAncillaryPO=new PatentApplicationAncillaryPO();
        BeanUtils.copyProperties(saveVO, patentApplicationAncillaryPO);
        patentApplicationAncillaryPO.setWcPatentApplicationId(patentApplicationPO.getId());
        applicationAncillaryDao.insertSelective(patentApplicationAncillaryPO);
        //添加联合出品单位
        if(saveVO.getUnit()!=null&&saveVO.getUnit().size()>0){
            for (WcPatentApplicationCompanyVO companyVO:saveVO.getUnit()){
                PatentApplicationCompanyPO patentApplicationCompanyPO=new PatentApplicationCompanyPO();
                BeanUtils.copyProperties(companyVO, patentApplicationCompanyPO);
                patentApplicationCompanyPO.setWcPatentApplicationId(patentApplicationPO.getId());
                applicationCompanyDao.insertSelective(patentApplicationCompanyPO);
            }
        }
        //添加流程
        CompanyInfoPO companyInfoPO=companyInfoDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());

        //创建一个新的流程任务
        examineService.addExamineState(new ExamineAddVO(patentApplicationPO.getId(), Constants.EXAMINE_SCRIPT_FILING[1],Constants.EXAMINE_1_USER,Constants.EXAMINE_TYPE_SCRIPT_FILING
                ,companyInfoPO.getId(),companyInfoPO.getUserName()),saveVO.getIsOutbox().equals("0")?1:0);


    }

    @Override
    public void editSave(WcPatentApplicationUpdateVO saveVO) {

        //处理主要信息
        PatentApplicationPO patentApplicationPO=applicationDao.selectByPrimaryKey(saveVO.getId());
        if(patentApplicationPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"备案信息未找到！");
        }
        BeanUtils.copyProperties(saveVO, patentApplicationPO);
        patentApplicationPO.setStatus(saveVO.getIsOutbox().equals("0")?1:0);
        patentApplicationPO.preUpdate();
        applicationDao.updateByPrimaryKeySelective(patentApplicationPO);
        //处理附件
        PatentApplicationAncillaryPO patentApplicationAncillaryPO=new PatentApplicationAncillaryPO();
        BeanUtils.copyProperties(saveVO, patentApplicationAncillaryPO);
        patentApplicationAncillaryPO.setWcPatentApplicationId(patentApplicationPO.getId());
        applicationAncillaryDao.updateByPrimaryKeySelective(patentApplicationAncillaryPO);

        //处理联合出品单位
        Example example=new Example(PatentApplicationCompanyPO.class);
        example.and().andEqualTo("wcPatentApplicationId",patentApplicationPO.getId());
        applicationCompanyDao.deleteByExample(example);
        if(saveVO.getUnit()!=null&&saveVO.getUnit().size()>0){
            for (WcPatentApplicationCompanyVO companyVO:saveVO.getUnit()){
                PatentApplicationCompanyPO patentApplicationCompanyPO=new PatentApplicationCompanyPO();
                BeanUtils.copyProperties(companyVO, patentApplicationCompanyPO);
                patentApplicationCompanyPO.setWcPatentApplicationId(patentApplicationPO.getId());
                applicationCompanyDao.insertSelective(patentApplicationCompanyPO);
            }
        }
        //处理流程
        if(saveVO.getIsOutbox().equals("0")){
            Example examineExample=new Example(ExaminePO.class);
            examineExample.and().andEqualTo("relatedId",patentApplicationPO.getId());
            ExaminePO examinePO= examineDao.selectOneByExample(examineExample);
            examinePO.setExamineState(1);
            examinePO.setCreateDate(new Date());
            examinePO.preUpdate();
            examineDao.updateByPrimaryKeySelective(examinePO);
            CompanyInfoPO companyInfoPO=companyInfoDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
            examineService.addExamineLog(new ExamineLogAddVO(examinePO.getRelatedId(), Constants.EXAMINE_LOG_AGREE,Constants.EXAMINE_ONE_NODE,companyInfoPO.getUserName()
                    ,companyInfoPO.getId(),"提交审核",null,null,examinePO.getId()));
        }


    }

    @Override
    public WcPatentApplicationVO getById(String id) {
        WcPatentApplicationVO wcPatentApplicationVO=new WcPatentApplicationVO();
        Example exampleEx=new Example(ExaminePO.class);
        exampleEx.and().andEqualTo("relatedId",id);
        ExaminePO examinePO=examineDao.selectOneByExample(exampleEx);
        wcPatentApplicationVO.setFlowId(examinePO.getId());

        PatentApplicationPO patentApplicationPO=applicationDao.selectByPrimaryKey(id);
        if(patentApplicationPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"备案信息未找到！");
        }
        BeanUtils.copyProperties(patentApplicationPO, wcPatentApplicationVO);

        Example example1=new Example(PatentApplicationAncillaryPO.class);
        example1.and().andEqualTo("wcPatentApplicationId",patentApplicationPO.getId());
        PatentApplicationAncillaryPO applicationAncillaryPO=applicationAncillaryDao.selectOneByExample(example1);
        BeanUtils.copyProperties(applicationAncillaryPO, wcPatentApplicationVO);

        Example example=new Example(PatentApplicationCompanyPO.class);
        example.and().andEqualTo("wcPatentApplicationId",patentApplicationPO.getId());
        List<PatentApplicationCompanyPO> patentApplicationCompanyPOList=applicationCompanyDao.selectByExample(example);
        List<WcPatentApplicationCompanyVO> list=new ArrayList<>();
        for (PatentApplicationCompanyPO companyPO:patentApplicationCompanyPOList){
            WcPatentApplicationCompanyVO wcPatentApplicationCompanyVO=new WcPatentApplicationCompanyVO();
            BeanUtils.copyProperties(companyPO, wcPatentApplicationCompanyVO);
            list.add(wcPatentApplicationCompanyVO);
        }
        wcPatentApplicationVO.setUnit(list);
        return wcPatentApplicationVO;
    }

    @Override
    public void removeById(String id) {
        PatentApplicationPO patentApplicationPO=applicationDao.selectByPrimaryKey(id);
        if(patentApplicationPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"备案信息未找到！");
        }
        Example exampleExamine=new Example(ExaminePO.class);
        exampleExamine.and().andEqualTo("relatedId",patentApplicationPO.getId()).andEqualTo("examineState",0);
        examineDao.deleteByExample(exampleExamine);
        applicationDao.deleteByPrimaryKey(id);
        Example example1=new Example(PatentApplicationAncillaryPO.class);
        example1.and().andEqualTo("wcPatentApplicationId",patentApplicationPO.getId());
        applicationAncillaryDao.deleteByExample(example1);
        Example example=new Example(PatentApplicationCompanyPO.class);
        example.and().andEqualTo("wcPatentApplicationId",patentApplicationPO.getId());
        applicationCompanyDao.deleteByExample(example);
    }



    @Autowired
    ScriptRecordInfoDao scriptRecordInfoDao;

    @Override
    public List<PageScriptRecordVO> scriptRecordPublicityInfo(ScriptRecordSearchVO searchVO) {
        return scriptRecordInfoDao.getList(searchVO);
    }

    @Override
    public List<WcContentExamineListVO> getList(WcContentExamineSearchVO searchVO) {
        return examineDao.getWcContentExamineList(searchVO);
    }


    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private UserMessagesDao userMessagesDao;


    @Override
    public void completeTask(ExamineCompleteVO taskCompleteDto) {
        ExaminePO examinePO=examineDao.selectByPrimaryKey(taskCompleteDto.getId());
        if(examinePO==null){
            throw new BusinessException(Type.FAIL.getCode(),"审核信息未找到！");
        }
        if(examinePO.getExamineState()!=1){
            throw new BusinessException(Type.FAIL.getCode(),"流程不是待审核状态，请勿重复操作！");
        }
        SysUserPO sysUserPO=sysUserDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
        if(sysUserPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"用户信息未找到！");
        }
        if(examinePO.getExamineUserIds().indexOf(sysUserPO.getId())<0){
            throw new BusinessException(Type.FAIL.getCode(),"当前用户不是该节点的审批人！");
        }
        String EXAMINE_LOG_AGREE=Constants.EXAMINE_LOG_AGREE;
        String EXAMINE_ONE_NODE=examinePO.getExamineNode();
        CompanyInfoPO companyInfoPO=companyInfoDao.selectByPrimaryKey(examinePO.getCreateBy());
        PatentApplicationPO patentApplicationPO=applicationDao.selectByPrimaryKey(examinePO.getRelatedId());


        if(examinePO.getExamineNode().equals(Constants.EXAMINE_SCRIPT_FILING[4])&&taskCompleteDto.getOutcome()==1){
            //初审进入下一阶段
            examinePO.setExamineState(2);
            examinePO.setExamineUserIds("");
            examinePO.setExamineNode(Constants.EXAMINE_SCRIPT_FILING[5]);
            examinePO.preUpdate();
            examineDao.updateByPrimaryKeySelective(examinePO);

            new UserMessageThread(userMessagesDao,"1",
                    "贵单位【"+companyInfoPO.getCompanyName()+"】提交的电影【"+patentApplicationPO.getMovieName()+"】剧本（梗概）备案已获国家电影局公示，同意拍摄。在“个人中心-办事记录”中可查看批复的备案回执单，也可联系市电影局（电话：023-63896874）",
                    companyInfoPO.getId()).start();
        }

        if(examinePO.getExamineNode().equals(Constants.EXAMINE_SCRIPT_FILING[3])&&taskCompleteDto.getOutcome()==1){
            //初审进入下一阶段
            examinePO.setExamineState(1);
            examinePO.setExamineUserIds(Constants.EXAMINE_2_USER);
            examinePO.setExamineNode(Constants.EXAMINE_SCRIPT_FILING[4]);
            examinePO.preUpdate();
            examineDao.updateByPrimaryKeySelective(examinePO);
            new UserMessageThread(userMessagesDao,"1",
                    "贵单位【"+companyInfoPO.getCompanyName()+"】提交的电影【"+patentApplicationPO.getMovieName()+"】剧本（梗概）备案已经重庆市电影局审查通过，提交至国家电影局审理。请留意国家电影局官方网站（https://www.chinafilm.gov.cn）的公示通知。",
                    companyInfoPO.getId()).start();
        }
        if(examinePO.getExamineNode().equals(Constants.EXAMINE_SCRIPT_FILING[2])&&taskCompleteDto.getOutcome()==1){
            //审查决定进入下一阶段
            EXAMINE_LOG_AGREE=Constants.EXAMINE_LOG_AGREE_S_P;
            examinePO.setExamineState(1);
            Example example1=new Example(ExamineLogPO.class);
            example1.and().andEqualTo("examineId",examinePO.getId()).andEqualTo("examineUserId",sysUserPO.getId());
            if(examineLogDao.selectCountByExample(example1)>0){
                throw new BusinessException(Type.FAIL.getCode(),"已经评审，请勿重复操作！");
            }
            Example example=new Example(ExamineLogPO.class);
            example.and().andEqualTo("examineId",examinePO.getId()).andEqualTo("examineNode",Constants.EXAMINE_SCRIPT_FILING[2]);
            //评审完毕进入下一阶段
            if( examinePO.getExamineUserIds().split(",").length-1==examineLogDao.selectCountByExample(example)){
                examinePO.setExamineUserIds(Constants.EXAMINE_1_USER);
                examinePO.setExamineNode(Constants.EXAMINE_SCRIPT_FILING[3]);
                examinePO.preUpdate();
                examineDao.updateByPrimaryKeySelective(examinePO);
            }
        }
        if(examinePO.getExamineNode().equals(Constants.EXAMINE_SCRIPT_FILING[1])&&taskCompleteDto.getOutcome()==1){
            //审查决定进入下一阶段
            EXAMINE_LOG_AGREE="受理不指派专家评审";
            examinePO.setExamineState(1);
            examinePO.setExamineUserIds(Constants.EXAMINE_1_USER);
            examinePO.setExamineNode(Constants.EXAMINE_SCRIPT_FILING[3]);
            examinePO.preUpdate();
            examineDao.updateByPrimaryKeySelective(examinePO);
        }

        if(taskCompleteDto.getOutcome()!=1){
            //审核失败  取消流程
            examinePO.setExamineState(taskCompleteDto.getOutcome()+1);
            examinePO.preUpdate();
            EXAMINE_LOG_AGREE=taskCompleteDto.getOutcome()==2?Constants.EXAMINE_LOG_DISAGREE:Constants.EXAMINE_LOG_RETURN;
            examineDao.updateByPrimaryKeySelective(examinePO);

            if(examinePO.getExamineNode().equals(Constants.EXAMINE_SCRIPT_FILING[1])){
                new UserMessageThread(userMessagesDao,"1",
                        "贵单位【"+companyInfoPO.getCompanyName()+"】提交的电影【"+patentApplicationPO.getMovieName()+"】剧本（梗概）备案经审核被退回，请修改后重新提交。",
                        companyInfoPO.getId()).start();
            }
            if(examinePO.getExamineNode().equals(Constants.EXAMINE_SCRIPT_FILING[3])&&taskCompleteDto.getOutcome()==2){
                new UserMessageThread(userMessagesDao,"1",
                        "贵单位【"+companyInfoPO.getCompanyName()+"】提交的电影【"+patentApplicationPO.getMovieName()+"】剧本（梗概）备案经审核未能通过。",
                        companyInfoPO.getId()).start();
            }
            if(examinePO.getExamineNode().equals(Constants.EXAMINE_SCRIPT_FILING[3])&&taskCompleteDto.getOutcome()==3){
                new UserMessageThread(userMessagesDao,"1",
                        "贵单位【"+companyInfoPO.getCompanyName()+"】提交的电影【"+patentApplicationPO.getMovieName()+"】剧本（梗概）备案经审核被退回。请修改后重新提交。",
                        companyInfoPO.getId()).start();
            }

        }
        examineService.addExamineLog(new ExamineLogAddVO(examinePO.getRelatedId(), EXAMINE_LOG_AGREE,EXAMINE_ONE_NODE,sysUserPO.getUserName()
                ,sysUserPO.getId(),taskCompleteDto.getComment(),taskCompleteDto.getApprovalFileUrl(),taskCompleteDto.getApprovalFileName(),examinePO.getId()));

    }

    @Override
    public void completeTaskUser(ExamineCompleteUserVO taskCompleteDto) {
        ExaminePO examinePO=examineDao.selectByPrimaryKey(taskCompleteDto.getId());
        if(examinePO==null){
            throw new BusinessException(Type.FAIL.getCode(),"审核信息未找到！");
        }
        if(examinePO.getExamineState()!=1){
            throw new BusinessException(Type.FAIL.getCode(),"流程不是待审核状态，请勿重复操作！");
        }
        SysUserPO sysUserPO=sysUserDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
        if(sysUserPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"审核用户信息未找到！");
        }
        if(examinePO.getExamineUserIds().indexOf(sysUserPO.getId())<0){
            throw new BusinessException(Type.FAIL.getCode(),"当前用户不是该节点的审批人！");
        }
        if(taskCompleteDto.getUserIds()==null&&taskCompleteDto.getUserIds().size()==0){
            throw new BusinessException(Type.FAIL.getCode(),"请指派专家信息！");
        }
        StringBuffer userIds=new StringBuffer();
        for (String userId:taskCompleteDto.getUserIds()){
            userIds.append(userId).append(",");
        }
        String EXAMINE_LOG_AGREE="受理并指派专家评审";
        String EXAMINE_ONE_NODE=examinePO.getExamineNode();
        //审查决定进入下一阶段
        examinePO.setExamineState(1);
        examinePO.setExamineUserIds(userIds.substring(0,userIds.length()-1));
        examinePO.setExamineNode(Constants.EXAMINE_SCRIPT_FILING[2]);
        examinePO.preUpdate();
        examineDao.updateByPrimaryKeySelective(examinePO);
        examineService.addExamineLog(new ExamineLogAddVO(examinePO.getRelatedId(), EXAMINE_LOG_AGREE,EXAMINE_ONE_NODE,sysUserPO.getUserName()
                ,sysUserPO.getId(),taskCompleteDto.getComment()==null?"指派专家评审":taskCompleteDto.getComment(),taskCompleteDto.getApprovalFileUrl(),taskCompleteDto.getApprovalFileName(),examinePO.getId()));
    }

    @Override
    public WcPatentApplicationDetailVO selectWcPatentApplicationDetailVOById(String id) {


        ExaminePO examinePO= examineDao.selectByPrimaryKey(id);

        WcPatentApplicationDetailVO wcPatentApplicationVO=new WcPatentApplicationDetailVO();
        wcPatentApplicationVO.setListOne(examineDao.getWcContentExamineOne(examinePO.getId()));

        PatentApplicationPO patentApplicationPO=applicationDao.selectByPrimaryKey(examinePO.getRelatedId());
        if(patentApplicationPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"备案信息未找到！");
        }
        BeanUtils.copyProperties(patentApplicationPO, wcPatentApplicationVO);
        Example example1=new Example(PatentApplicationAncillaryPO.class);
        example1.and().andEqualTo("wcPatentApplicationId",patentApplicationPO.getId());
        PatentApplicationAncillaryPO applicationAncillaryPO=applicationAncillaryDao.selectOneByExample(example1);
        BeanUtils.copyProperties(applicationAncillaryPO, wcPatentApplicationVO);
        Example example=new Example(PatentApplicationCompanyPO.class);
        example.and().andEqualTo("wcPatentApplicationId",patentApplicationPO.getId());
        List<PatentApplicationCompanyPO> patentApplicationCompanyPOList=applicationCompanyDao.selectByExample(example);
        List<WcPatentApplicationCompanyVO> list=new ArrayList<>();
        for (PatentApplicationCompanyPO companyPO:patentApplicationCompanyPOList){
            WcPatentApplicationCompanyVO wcPatentApplicationCompanyVO=new WcPatentApplicationCompanyVO();
            BeanUtils.copyProperties(companyPO, wcPatentApplicationCompanyVO);
            wcPatentApplicationCompanyVO.setLegalPersonName(companyPO.getLegalPersonName());
            list.add(wcPatentApplicationCompanyVO);
        }
        wcPatentApplicationVO.setUnit(list);
        Example example2=new Example(CompanyInfoPO.class);
        example2.and().andEqualTo("companyName",patentApplicationPO.getProductionUnit());
        CompanyInfoPO companyInfoPO=companyInfoDao.selectOneByExample(example2);
        CulvertApprovalCompanyVo culvertApprovalCompanyVo=new CulvertApprovalCompanyVo();
        culvertApprovalCompanyVo.setCompanyId(companyInfoPO.getId());
        culvertApprovalCompanyVo.setBusinessLicense(companyInfoPO.getBusinessLicense());
        culvertApprovalCompanyVo.setLegalPersonName(companyInfoPO.getUserName());
        culvertApprovalCompanyVo.setLegalPersonPhone(companyInfoPO.getUserPhone());
        culvertApprovalCompanyVo.setCompanyName(companyInfoPO.getCompanyName());

        wcPatentApplicationVO.setCompanyVo(culvertApprovalCompanyVo);

        //查询审核记录
        Example example3=new Example(ExamineLogPO.class);
        example3.and().andEqualTo("examineId",examinePO.getId());
        example3.setOrderByClause("examine_date desc");
        List<ExamineLogPO> examineLogPOS=examineLogDao.selectByExample(example3);
        List<ExamineLogVO> examineLogVOList=new ArrayList<>();
        for (ExamineLogPO examineLogPO:examineLogPOS){
            ExamineLogVO examineLogVO=new ExamineLogVO();
            BeanUtils.copyProperties(examineLogPO, examineLogVO);
            examineLogVOList.add(examineLogVO);
        }
        wcPatentApplicationVO.setExamineLogList(examineLogVOList);




        //查询提交记录
        List<SubmitLogVO> submitLogVOS=examineDao.getPatentSubmitLog(wcPatentApplicationVO.getNumber());
        Integer order=0;
        for (SubmitLogVO submitLogVO:submitLogVOS){
            submitLogVO.setSubmitOrder("第"+(submitLogVOS.size()-order)+"次提交");
            order++;
        }
        wcPatentApplicationVO.setSubmitLogList(submitLogVOS);
        return wcPatentApplicationVO;
    }

    @Override
    public List<WcPatentApplicationRecommendationVO> getRecommendationList(IDEntity saveVO) {
        if(StringUtils.isBlank(saveVO.getId())){
            return  new ArrayList<>();
        }
        return  applicationDao.getRecommendationList(saveVO.getId());
    }

    @Override
    public void addRecommendation(WcPatentApplicationRecommendationAddVO saveVO) {
        WcPatentApplicationRecommendationVO wcPatentApplicationRecommendationVO=new WcPatentApplicationRecommendationVO();
        BeanUtils.copyProperties(saveVO, wcPatentApplicationRecommendationVO);
        wcPatentApplicationRecommendationVO.setRecommendationTime(new Date());

        PatentApplicationPO patentApplicationPO=applicationDao.selectByPrimaryKey(saveVO.getWcPatentApplicationId());
        CompanyInfoPO companyInfoPO=companyInfoDao.selectByPrimaryKey(patentApplicationPO.getCreateBy());
        new UserMessageThread(userMessagesDao,"1",
                "贵单位【"+companyInfoPO.getCompanyName()+"】提交的电影【"+patentApplicationPO.getMovieName()+"】剧本（梗概）备案未通过国家电影局审查，在“个人中心-办事记录”中查看国家电影局修改反馈意见，后续请在国家电影局电子政务平台（https://www.chinafilm.gov.cn）上进行修改及提交操作。",
                companyInfoPO.getId()).start();

        applicationDao.saveRecommendation(wcPatentApplicationRecommendationVO);
    }


    @Autowired
    WcContentExamineDao wcContentExamineDao;

    @Override
    public void addRecommendationExamine(WcPatentApplicationRecommendationAddVO saveVO) {
        WcPatentApplicationRecommendationVO wcPatentApplicationRecommendationVO=new WcPatentApplicationRecommendationVO();
        BeanUtils.copyProperties(saveVO, wcPatentApplicationRecommendationVO);
        wcPatentApplicationRecommendationVO.setRecommendationTime(new Date());

        WcContentExaminePO wcContentExaminePO=wcContentExamineDao.selectByPrimaryKey(saveVO.getWcPatentApplicationId());
        CompanyInfoPO companyInfoPO=companyInfoDao.selectByPrimaryKey(wcContentExaminePO.getCreateBy());
        new UserMessageThread(userMessagesDao,"1",
                "贵单位【"+companyInfoPO.getCompanyName()+"】提交的电影【"+wcContentExaminePO.getMovieName()+"】未通过国家电影局审查，你可点击【链接】查看国家电影局反馈意见，后续请在国家电影局电子政务平台查看并处理相关问题。",
                companyInfoPO.getId()).start();

        applicationDao.saveRecommendation(wcPatentApplicationRecommendationVO);
    }

    @Override
    public void saveReceipt(ScriptRecordAddVO addVO) {
        ScriptRecordInfoPO scriptRecordInfoPO=new ScriptRecordInfoPO();
        BeanUtils.copyProperties(addVO, scriptRecordInfoPO);
        scriptRecordInfoPO.preInsert();;
        scriptRecordInfoPO.setNoticeTime(scriptRecordInfoPO.getCreateDate());
        scriptRecordInfoDao.insertSelective(scriptRecordInfoPO);
    }

    @Override
    public void updateReceipt(ScriptRecordUpdateVO updateVO) {
        ScriptRecordInfoPO scriptRecordInfoPO=scriptRecordInfoDao.selectByPrimaryKey(updateVO.getId());
        if(scriptRecordInfoPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"备案信息未找到！");
        }
        BeanUtils.copyProperties(updateVO, scriptRecordInfoPO);
        scriptRecordInfoPO.preUpdate();
        scriptRecordInfoDao.updateByPrimaryKeySelective(scriptRecordInfoPO);
    }

    @Override
    public void removeReceiptById(String id) {
        ScriptRecordInfoPO scriptRecordInfoPO=scriptRecordInfoDao.selectByPrimaryKey(id);
        if(scriptRecordInfoPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"备案信息未找到！");
        }
        scriptRecordInfoDao.deleteByPrimaryKey(id);
    }

    @Override
    public void removeReceiptByIds(List<String> ids) {
        for (String id:ids) {
            this.removeReceiptById(id);
        }
    }
}